import { observable, action, autorun, toJS } from 'mobx';
import { WeaTableNew, WeaForm } from 'comsMobx';
import { WeaLocaleProvider } from 'ecCom';
import { message, Button } from 'antd';
import moment from 'moment';
import * as Apis from './../../apis/remind';

const getLabel = WeaLocaleProvider.getLabel;
const { TableStore } = WeaTableNew;

export default class AddRemindForm {
    comObj = {};
    params = {};
    @observable isSuccess = false;
    @observable loading = true;
    @observable msgCode = '';
    // @observable form = {};
    @observable currentPath = {};
    @observable receiverBroswer = {};
    @observable receiverBroswerValue = [];// 接收人
    @observable receiver = '';
    @observable chanceBroswer = {};
    @observable chanceBroswerValue = [];// 商机
    @observable customerBroswerValue = [];// 客户
    @observable contacterBroswerValue = [];// 联系人
    @observable sellChanceId = '';
    @observable sendWay = 1;// 发送方式,1:应用内；2：短信
    @observable timedSende = false;// 定时发送
    @observable senderTime = '';// 定时发送时间
    @observable textareaValue = '';// 输入框的值
    @observable commonLanguage = '';// 输入框的值
    @observable commonLanguageList = [];
    @observable customerid = '';
    @observable contacterid = '';

    @action
    doNew = (comObj) => {
      this.comObj = comObj;
      const now = moment().format('YYYY-MM-DD HH:MM');
      this.senderTime = now;
      if (comObj.props.params) {
        const p = comObj.props.params;
        if (p.receiver) {
          this.receiverBroswerValue = [{ id: p.receiver, name: p.receiverspan }];
          this.receiver = p.receiver;
        }
        if (p.sellchanceid) {
          this.chanceBroswerValue = [{ id: p.sellchanceid, name: p.sellchancespan }];
          this.sellChanceId = p.sellchanceid;
        }
        if (p.customerid) {
          this.customerBroswerValue = [{ id: p.customerid, name: p.customerspan }];
          this.customerid = p.customerid;
        }
      }
      this.getCommonLanguageList();
    }
    // 获取人员id
    @action
    getHrm = (params) => {
      const p = [];
      const value = [];
      params.forEach((key) => {
        p.push(key.id);
        value.push({ id: key.id, name: key.lastname });
      });
      this.receiver = `${p}`;
      this.receiverBroswerValue = value;
    }

    @action
    getSellChance = (params) => {
      const p = [];
      const cp = [];
      const value = [];
      const customerBroswerValue = [];
      params.forEach((key) => {
        p.push(key.id);
        cp.push(key.customerid);
        value.push({ id: key.id, name: key.subject || key.name });
        customerBroswerValue.push({ id: key.customerid, name: key.customeridspan });
      });
      this.sellChanceId = `${p}`;
      if (this.customerid == '') {
        this.customerid = `${cp}`;
        this.customerBroswerValue = customerBroswerValue;
      }
      this.chanceBroswerValue = value;
    }
    @action
    getCustomer = (params) => {
      const p = [];
      const value = [];
      params.forEach((key) => {
        p.push(key.id);
        value.push({ id: key.id, name: key.name });
      });
      if (this.customerid !== `${p}`) {
        this.sellChanceId = '';
        this.chanceBroswerValue = [];
        this.contacterid = '';
        this.contacterBroswerValue = [];
      }
      this.customerid = `${p}`;
      this.customerBroswerValue = value;
    }

    @action
    getContacter = (params) => {
      // console.log(params)
      const p = [];
      const cp = [];
      const value = [];
      const customerBroswerValue = [];
      params.forEach((key) => {
        // console.log(key)
        p.push(key.id);
        cp.push(key.customerid);
        value.push({ id: key.id, name: key.firstname || key.name });
        customerBroswerValue.push({ id: key.customerid, name: key.namespan });
      });
      // console.log(this.customerid)
      this.contacterid = `${p}`;
      this.contacterBroswerValue = value;
      if (this.customerid == '') {
        this.customerid = `${cp}`;
        this.customerBroswerValue = customerBroswerValue;
        // console.log(customerBroswerValue)
      }
    }
    /**
     * 保存提醒
     */
    @action
    saveAddRemind = () => {
      // 如果输入框内容为空，则提示
      if (this.textareaValue == '') {
        message.error(getLabel('514792', '提醒内容不能为空'));
        return;
      }
      // 如果接收人内容为空，则提示
      if (this.receiver == '') {
        message.error(getLabel('514793', '接收人不能为空'));
        return;
      }
      const now = moment().format('YYYY-MM-DD HH:mm:ss');
      if (this.timedSende) {
        const date3 = moment(this.senderTime).diff(now, 'minute');// 计算相差的分钟数
        if (date3 < 0) {
          message.error(getLabel('515270', '定时发送时间不能小于当前时间'));
          return;
        }
      }
      let type = '';
      if (this.sellChanceId == '') {
        type = '2';
      } else {
        type = '0';
      }
      const params = {
        content: this.textareaValue,
        receiver: this.receiver,
        sellchanceid: this.sellChanceId,
        type,
        sendWay: this.sendWay,
        senderTime: this.senderTime,
        timedSende: this.timedSende,
        customer: this.customerid,
        contacterid: this.contacterid,
      };
      // console.log(params);

      // if (this.form.doValidate().pass) {
      Apis.saveRemind(params).then((data) => {
        if (data.status == 'success') {
          message.success(getLabel('514794', '提醒成功'));
          // setTimeout(function () {
          this.comObj.props.onDiaCloseClick && this.comObj.props.onDiaCloseClick(1);
          // }, 3000);
        } else {
          message.error(data.msgCode);
        }
      });
      // }
    }

    // 是否定时发送
    @action
    timedSendeChange = (value) => {
      this.senderTime = moment().format('YYYY-MM-DD HH:mm:ss');
      this.timedSende = value;
    }
    // 发送方式切换
    @action
    sendWayChange = (value) => {
      if (value == '2') {
        this.sendWay = 2;
      } else if (value == '1') {
        this.sendWay = 1;
      }
    }
    // 日期-时间选择
    @action
    dataPickChange = (value) => {
      // console.log(value)
      this.senderTime = value;
    }

    // 改变输入框的值
    @action
    textareaValueChange = (value) => {
      this.textareaValue = value;
    }
    // 改变常用语的值
    @action
    commonLanguageChance = (value) => {
      this.commonLanguage = value;
    }

    // 获取快捷输入的内容
    @action
    getCommonLanguageList = () => {
      this.loading1 = true;
      this.isSuccess1 = false;
      Apis.getCommonLanguageList({ content: this.textareaValue }).then((data) => {
        this.commonLanguageList = data.commonLanguageList;
        // console.log(data.status);
        this.isSuccess1 = true;
        this.loading1 = false;
      });
    }

    // 添加常用回复
    @action
    submitCommomReply = () => {
      if (this.commonLanguage == '' || this.commonLanguage == 'undefined') {
        message.error(getLabel('514795', '常用语不能为空'), 1);
        return;
      }
      // console.log(this.commonLanguage);
      const params = { type: 1, text: this.removeHtmlTab(this.commonLanguage) };
      Apis.saveCommonLanguage(params).then((data) => {
        if (data.status == 'success') {
          message.success(getLabel('514796', '常用语保存成功'), 1);
          this.getCommonLanguageList();
          this.comObj.setState({ lvisible: false });
          this.commonLanguage = '';
        }
      });
    }
    // 去掉HTML标签
    removeHtmlTab = tab =>
      tab.replace(/<[^<>]+?>/g, '')// 删除所有HTML标签

    // 评论的点击次数加一
    @action
    saveCommonLanguageCount = (params) => {
      Apis.saveCommonLanguageCount(params).then((data) => {
        // console.log(data)
      });
    }
    // 删除常用语
    @action
    deleteCommonLanguageCount = (params) => {
      Apis.deleteCommonLanguageCount(params).then((data) => {
        // console.log(data)
      });
    }
    @action
    resetForm = () => {
      this.textareaValue = '';
      this.sendWay = 1;
      this.timedSende = false;
      this.senderTime = moment().format('YYYY-MM-DD HH:MM');
      this.receiverBroswerValue = [];
      this.receiver = '';
      this.chanceBroswerValue = [];
      this.sellChanceId = '';
      this.customerBroswerValue = [];
      this.customerid = '';
      this.contacterBroswerValue = [];
      this.contacterid = '';
    }
}
